package com.jay.io.clazz;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class ByteArrayStreamCls {
	public static void main(String[] args) throws IOException {
		try (ByteArrayOutputStream bout = new ByteArrayOutputStream();) {
			int a = 0;
			int b = 1;
			int c = 2;
			bout.write(a);
			bout.write(b);
			bout.write(c);
			byte[] buff = bout.toByteArray();
			for (int i = 0; i < buff.length; i++)
				System.out.println(buff[i]);
			System.out.println("***********************");
			ByteArrayInputStream bin = new ByteArrayInputStream(buff);
			while ((b = bin.read()) != -1) {
				System.out.println(b);
			}

			System.out.println("The current buff size is " + bout.size());
			bout.reset();
			String text = "abcdefg";
			bout.write(text.getBytes());
			System.out.println(bout);
			bout.writeTo(new FileOutputStream("out.txt"));

			System.out.println("***********************");
			bin = new ByteArrayInputStream(text.getBytes());
			System.out.println("The current available size is "
					+ bin.available());
			System.out.println((char) bin.read());
			bin.skip(2);
			System.out.println((char) bin.read());
			System.out.println("***********************");
			bin.reset();
			while ((b = bin.read()) != -1) {
				System.out.println((char) b);
			}
			System.out.println("***********************");
			if (bin.markSupported()) {
				// 参数没有用，mark此时设置到了pos，也就是最后一位
				bin.mark(2);
				// reset到了mark，也就是末尾，所以无输出
				System.out.println("The current available size is "
						+ bin.available());
				bin.reset();
				System.out.println("The current available size is "
						+ bin.available());
				while ((b = bin.read()) != -1) {
					System.out.println((char) b);
				}
			}
			// 空方法
			bout.close();
			bin.close();
			bout.flush();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}
